package com.hilon.foundation.base;

import com.hilon.foundation.para.ConditionPara;
import com.hilon.foundation.util.HilonTools;
import com.hilon.foundation.util.ResultBean;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public abstract class BaseServiceImpl<T extends EntityBaseId, V> {
    protected final EntityManager entityManager;
    protected String entityName;
    protected String viewName;

    protected BaseServiceImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Transactional
    public ResultBean<String> append(T entity) {
        try {
            entity.setIsDeleted(false);
            entityManager.merge(entity);
            return new ResultBean<>(200, "成功增加记录");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501, "增加记录失败");
        }
    }

    @Transactional
    public ResultBean<String> deleteById(Long idPara) {
        String sql = "update " + entityName + " e set e.isDeleted=true where e.id=?1";
        try {
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, idPara);
            int i = query.executeUpdate();
            if (i > 0) return new ResultBean<>(200, "删除成功");
            else return new ResultBean<>(501, "删除失败");
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "删除程序失败");
        }
    }

    @Transactional
    public ResultBean<String> deleteByIds(List<Long> ids) {
        String idsStr = HilonTools.getIdsStr(ids);

        String sql = "update " + entityName + " e set e.isDeleted=true " + "where e.id in (" + idsStr + ") ";

        try {
            Query query = entityManager.createQuery(sql);
            int i = query.executeUpdate();
            if (i > 0) return new ResultBean<>(200, "删除成功");
            else return new ResultBean<>(501, "删除失败,该记录已审核");
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "删除程序失败");
        }
    }

    @Transactional
    public ResultBean<String> edit(T entity) {
        try {
            T entity2 = entityManager.merge(entity);
            return new ResultBean<>(200, "成功修改记录");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501, "修改失败");
        }
    }

    public ResultBean<V> findById(Long idPara) {
        System.out.println("找对应的id，将表格数据传回前端，以下是id值");
        System.out.println(idPara);
        String sql = "select e from " + viewName + " e where e.id = ?1 " + "and e.isDeleted=false ";
        try {
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, idPara);
            List<V> list = (List<V>) query.getResultList();
            System.out.println("list.get(0)=" + list.get(0));
            if (!list.isEmpty()) return new ResultBean<>(list.get(0));
            else return new ResultBean<>(404, "没找到ID:" + idPara + "对应的主表数据行");
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "findById执行错误");
        }
    }

    public ResultBean<List<V>> findAll() {
        String sql = "select e from " + viewName + " e where e.isDeleted=false ";
        try {
            System.out.println("findAll被执行");
            System.out.println(sql);
            Query query = entityManager.createQuery(sql);
            List<V> list = (List<V>) query.getResultList();
            System.out.println(list);
            return new ResultBean<>(list);
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "findAll执行错误");
        }
    }

    public ResultBean<List<V>> findByCondition(ConditionPara conditionPara) {
        String sql = "select e from " + viewName + " e where e.isDeleted=false " + "and e.categoryType like ?1 " + "and e.categoryTypeDetail like ?2";
        try {
            System.out.println("findByCondition查询语句执行");
            System.out.println(sql);
            ConditionPara.formatData(conditionPara);
            System.out.println(conditionPara);
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, conditionPara.getCategoryType());
            query.setParameter(2, conditionPara.getCategoryTypeDetail());
            List<V> list = (List<V>) query.getResultList();
            System.out.println(list);
            return new ResultBean<>(list);
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "findByCondition执行错误");
        }
    }

    public ResultBean<List<V>> findAllWithDeleted() {
        String sql = "select e from " + viewName + " e ";
        try {
            Query query = entityManager.createQuery(sql);
            return (ResultBean<List<V>>) query.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "findAllWithDeleted执行错误");
        }
    }
}
